package com.wanghangcloud.common.config;


import com.wanghangcloud.common.utils.IPUtils;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import feign.codec.ErrorDecoder;
import io.micrometer.common.util.StringUtils;


import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

/**
 * feign配置
 */
@Slf4j
@Configuration
public class FeignConfig {

    /**
     * header传递
     * @return
     */
    @Bean
    public RequestInterceptor requestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                log.info("FeignConfig.requestInterceptor：远程调用微服务{}的拦截器执行开始<<<<<<", template.url());
                // 从上下文中获取 Token
                RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
                if (attributes != null) {
                    // 从 RequestAttributes 中获取 HttpServletRequest
                    HttpServletRequest request = (HttpServletRequest) attributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
                    if (request != null) {
                        log.info("FeignConfig.requestInterceptor：开始构造HTTP请求头");
                        // 从请求头中获取 Token
                        String token = request.getHeader("Authorization");
                        if (StringUtils.isNotBlank(token)) {
                            template.header("Authorization", token);
                        }

                        // 设置用户id
                        String userId = request.getHeader("user_id");
                        if (StringUtils.isNotBlank(userId)) {
                            template.header("user_id", userId);
                        }
                        String userName = request.getHeader("user_name");
                        if (StringUtils.isNotBlank(userName)) {
                            template.header("user_name", userName);
                        }
                        String deptId = request.getHeader("dept_id");
                        if (StringUtils.isNotBlank(deptId)) {
                            template.header("dept_id", deptId);
                        }
                        String dataScope = request.getHeader("data_scope");
                        if (StringUtils.isNotBlank(dataScope)) {
                            template.header("data_scope", dataScope);
                        }
                        String authorities = request.getHeader("authorities");
                        if (StringUtils.isNotBlank(authorities)) {
                            template.header("authorities", authorities);
                        }
                        String userType2 = request.getHeader("user_type");
                        if (StringUtils.isNotBlank(userType2)) {
                            template.header("user_type", userType2);
                        }
                        String name = request.getHeader("name");
                        if (StringUtils.isNotBlank(name)) {
                            template.header("name", name);
                        }
                        String mobile = request.getHeader("mobile");
                        if (StringUtils.isNotBlank(mobile)) {
                            template.header("mobile", mobile);
                        }

                        String appCode = request.getHeader("appCode");
                        if (StringUtils.isNotBlank(appCode)) {
                            template.header("appCode", appCode);
                        }
                        // 登录日志 appId terminalType userType
                        String appId = request.getHeader("appId");
                        if (StringUtils.isNotBlank(appId)) {
                            template.header("appId", appId);
                        }
                        // 登录日志 appId terminalType userType
                        String appName = request.getHeader("appName");
                        if (StringUtils.isNotBlank(appName)) {
                            template.header("appName", appName);
                        }

                        String terminalType = request.getHeader("terminalType");
                        if (StringUtils.isNotBlank(terminalType)) {
                            template.header("terminalType", terminalType);
                        }
                        String userType = request.getHeader("userType");
                        if (StringUtils.isNotBlank(userType)) {
                            template.header("userType", userType);
                        }
                        // 设置ip
                        String remoteIp = IPUtils.getIpAddr(request);
                        template.header("x-forwarded-for",remoteIp);
                    }
                    log.info("FeignConfig.requestInterceptor：远程调用微服务{}的拦截器执行结束>>>>>>", template.url());
                }
            }
        };
    }

    @Bean
    public ErrorDecoder errorDecoder() {
        return new CustomErrorDecoder();
    }
}
